package com.blog.dazi.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blog.dazi.utils.RandomDataGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    public Map<String, Object> findByTel(String tel) {
        String sql = "select * from sys_user where phone_number = ?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,tel);
        if(maps!=null && maps.size()>0){
            return maps.get(0);
        }else{
            String sql1 = "insert into sys_user (username,phone_number,localtion_id,photo_id,user_type,sex,dazi_number) values (?,?,?,?,?,?,?)";
            Map<String, Object> map = RandomDataGenerator.generateUserData();
            map.put("phone_number",tel);
            Random random = new Random();
            int randomNumber = random.nextInt(3179) + 1;
            try {
                jdbcTemplate.update(sql1, map.get("username"),map.get("phone_number"),randomNumber,
                        1,map.get("user_type")
                        ,map.get("sex"),map.get("dazi_number"));
                return map;
            }catch (Exception e){
                return new HashMap<>();
            }

        }

    }

    public Map<String, Object> findUserAllByTel(String tel) {
        String sql = "select * from sys_user a,sys_user_photo b where a.photo_id = b.id and a.phone_number = ?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,tel);
        if(maps!=null && maps.size()>0){
            return maps.get(0);
        }else{
            return new HashMap<>();
        }

    }

    public Map<String, Object> findUserAllByUserId(String userId) {
        String sql = "select * from sys_user a,sys_user_photo b where a.photo_id = b.id and a.id = ?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,userId);
        if(maps!=null && maps.size()>0){
            return maps.get(0);
        }else{
            return new HashMap<>();
        }
    }

    public Map<String, Object> findUserByDaziNumbere(String daziNumber) {
        String sql = "select * from sys_user a,sys_user_photo b where a.photo_id = b.id and a.dazi_number = ?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,daziNumber);
        if(maps!=null && maps.size()>0){
            return maps.get(0);
        }else{
            return new HashMap<>();
        }
    }

    public int updatePhoto(String user) {
        JSONObject jsonObject = JSON.parseObject(user);
        String tel = (String) jsonObject.get("tel");
        String sql = "select id from sys_user where phone_number = ?";
        String image = (String) jsonObject.get("image");;
        try {
            Map<String, Object> map1 = jdbcTemplate.queryForMap(sql, tel);
            Integer userId = (Integer) map1.get("id");
            String sql2 = "insert into sys_user_photo (photo) values (?)";
            KeyHolder keyHolder = new GeneratedKeyHolder();
            jdbcTemplate.update(connection -> {
                PreparedStatement ps = connection.prepareStatement(sql2, new String[] { "id" }); // 指定自动生成的字段
                ps.setObject(1, image);
                return ps;
            }, keyHolder);

            // 获取自动生成的 ID
            long generatedId = keyHolder.getKey().longValue();
            String sql3 = "update sys_user set photo_id = ? where id = ?";
            jdbcTemplate.update(sql3,generatedId,userId);
            return 1;
        }catch (Exception e){

        }
        return 0;
    }
}
